package com.zeng.controller;

import com.zeng.entity.Result;
import com.zeng.utils.IDGenerator;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

//import static com.sun.xml.internal.ws.spi.db.BindingContextFactory.LOGGER;


/**
 * 文件上传接口
 *
 * @author zengfanchao
 * @since 2021-03-17 19:17:07
 */
@RestController
@RequestMapping("UpData")
public class UpDataController {

    @Value("${cbs.imagesPath}")
    private String imagesPath;

    @PostMapping("upkindimg")
    public Result upload(@RequestParam("kimg") MultipartFile kimg) {
        if (kimg.isEmpty()) {
            return Result.error("文件为空");
        }
        String fileName = IDGenerator.generateUUID() + ".jpg";
        File dest = new File(imagesPath+"kinds/"+ fileName);
//        File dest = new File("/var/lib/jenkins/workspace/campus-trading-system-back/imgdata/kinds/" + fileName);
        try {
            kimg.transferTo(dest);
            return Result.success(fileName);
        } catch (IOException e) {
            return Result.error("上传失败");
        }
    }

    @PostMapping("upimg")
    public Result upimg(@RequestParam("img") MultipartFile img) {
        if (img.isEmpty()) {
            return Result.error("文件为空");
        }
        String fileName = IDGenerator.generateUUID() + ".jpg";
        File dest = new File(imagesPath+"others/" + fileName);
//        File dest = new File("/var/lib/jenkins/workspace/campus-trading-system-back/imgdata/others/" + fileName);
        try {
            img.transferTo(dest);
            return Result.success(fileName);
        } catch (IOException e) {
            return Result.error("上传失败");
        }
    }
}